<template>
  <div class="admin-top-navbar">
    <div class="navbar-left">
      <h1 class="app-title">
        <el-icon class="title-icon"><House /></el-icon>
        房源管理系统
      </h1>
    </div>
    
    <!-- <div class="navbar-center">
      <div class="system-status">
        <el-tag type="success" size="small">
          <el-icon><CircleCheck /></el-icon>
          系统正常
        </el-tag>
      </div>
    </div> -->
    
    <div class="navbar-right">
      <!-- 系统监控 -->
      <div class="system-monitor">
        <el-tooltip content="CPU使用率" placement="bottom">
          <div class="monitor-item">
            <el-icon class="monitor-icon"><Monitor /></el-icon>
            <div class="monitor-bar">
              <div class="monitor-fill cpu" :style="{ width: cpuUsage + '%' }"></div>
            </div>
            <span class="monitor-text">{{ cpuUsage }}%</span>
          </div>
        </el-tooltip>

        <el-tooltip content="内存使用率" placement="bottom">
          <div class="monitor-item">
            <el-icon class="monitor-icon"><DataBoard /></el-icon>
            <div class="monitor-bar">
              <div class="monitor-fill memory" :style="{ width: memoryUsage + '%' }"></div>
            </div>
            <span class="monitor-text">{{ memoryUsage }}%</span>
          </div>
        </el-tooltip>
      </div>

      <!-- 通知中心 -->
      <div class="notification-center">
        <el-badge :value="notificationCount" :max="99" class="notification-badge">
          <el-button circle class="notification-btn" @click="showNotifications">
            <el-icon class="notification-icon" :class="{ shake: hasNewNotification }"><Bell /></el-icon>
          </el-button>
        </el-badge>

        <!-- 消息弹窗 -->
        <el-popover
          v-model:visible="notificationVisible"
          placement="bottom-end"
          :width="350"
          trigger="manual"
          :hide-after="0"
          :popper-style="{ padding: '0' }"
        >
          <template #reference>
            <span></span>
          </template>
          <div class="notification-panel">
            <div class="notification-header">
              <span>通知中心</span>
              <el-button text @click="markAllRead">全部已读</el-button>
            </div>
            <div class="notification-list">
              <div
                v-for="notification in notifications"
                :key="notification.id"
                class="notification-item"
                :class="{ unread: !notification.read }"
                @click="markAsRead(notification)"
              >
                <div class="notification-icon-wrapper">
                  <el-icon :class="getNotificationIconClass(notification.type)">
                    <component :is="getNotificationIcon(notification.type)" />
                  </el-icon>
                </div>
                <div class="notification-content">
                  <div class="notification-title">{{ notification.title }}</div>
                  <div class="notification-desc">{{ notification.description }}</div>
                  <div class="notification-time">{{ notification.time }}</div>
                </div>
              </div>
            </div>
          </div>
        </el-popover>
      </div>

      <!-- 用户信息 -->
      <el-dropdown @command="handleUserCommand" class="user-dropdown">
        <div class="user-info">
          <div class="user-avatar-wrapper">
            <el-avatar
              :size="36"
              src="https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png"
              class="user-avatar"
            />
            <div class="online-indicator"></div>
          </div>
          <div class="user-details">
            <span class="username">超级管理员</span>
            <span class="user-role">Administrator</span>
          </div>
          <el-icon class="dropdown-icon"><ArrowDown /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu class="user-dropdown-menu">
            <el-dropdown-item command="profile" class="dropdown-item">
              <el-icon><User /></el-icon>
              <span>个人资料</span>
            </el-dropdown-item>
            <el-dropdown-item command="settings" class="dropdown-item">
              <el-icon><Setting /></el-icon>
              <span>系统设置</span>
            </el-dropdown-item>
            <el-dropdown-item command="theme" class="dropdown-item">
              <el-icon><Sunny /></el-icon>
              <span>主题切换</span>
            </el-dropdown-item>
            <el-dropdown-item command="logout" divided class="dropdown-item logout">
              <el-icon><SwitchButton /></el-icon>
              <span>退出登录</span>
            </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { adminLogout } from '@/utils/auth'
import {
  House,
  CircleCheck,
  Bell,
  User,
  Setting,
  SwitchButton,
  ArrowDown,
  Monitor,
  DataBoard,
  Sunny
} from '@element-plus/icons-vue'

const router = useRouter()

// 通知相关
const notificationCount = ref(5)
const notificationVisible = ref(false)
const hasNewNotification = ref(true)
let notificationTimer = null

// 系统监控
const cpuUsage = ref(45)
const memoryUsage = ref(68)

// 通知数据
const notifications = ref([
  {
    id: 1,
    type: 'success',
    title: '系统更新',
    description: '系统已成功更新到最新版本',
    time: '5分钟前',
    read: false
  },
  {
    id: 2,
    type: 'warning',
    title: '待审核申请',
    description: '有3个中介申请待审核',
    time: '10分钟前',
    read: false
  },
  {
    id: 3,
    type: 'info',
    title: '数据备份',
    description: '每日数据备份已完成',
    time: '1小时前',
    read: true
  },
  {
    id: 4,
    type: 'error',
    title: '登录异常',
    description: '检测到异常登录尝试',
    time: '2小时前',
    read: false
  },
  {
    id: 5,
    type: 'info',
    title: '系统维护',
    description: '系统将于今晚进行维护',
    time: '3小时前',
    read: true
  }
])

let monitorInterval = null

// 显示通知
const showNotifications = () => {
  // 清除之前的定时器
  if (notificationTimer) {
    clearTimeout(notificationTimer)
    notificationTimer = null
  }

  notificationVisible.value = !notificationVisible.value
  if (notificationVisible.value) {
    hasNewNotification.value = false
    // 5秒后自动关闭
    notificationTimer = setTimeout(() => {
      notificationVisible.value = false
      notificationTimer = null
    }, 5000)
  }
}

// 获取通知图标
const getNotificationIcon = (type) => {
  const icons = {
    success: 'CircleCheck',
    warning: 'Bell',
    info: 'Bell',
    error: 'Bell'
  }
  return icons[type] || 'Bell'
}

// 获取通知图标样式
const getNotificationIconClass = (type) => {
  const classes = {
    success: 'notification-success',
    warning: 'notification-warning',
    info: 'notification-info',
    error: 'notification-error'
  }
  return classes[type] || 'notification-info'
}

// 标记为已读
const markAsRead = (notification) => {
  notification.read = true
  updateNotificationCount()
}

// 全部标记为已读
const markAllRead = () => {
  notifications.value.forEach(n => n.read = true)
  updateNotificationCount()
  ElMessage.success('所有通知已标记为已读')
}

// 更新通知数量
const updateNotificationCount = () => {
  notificationCount.value = notifications.value.filter(n => !n.read).length
}

// 更新系统监控数据
const updateMonitorData = () => {
  // 模拟CPU和内存使用率变化
  cpuUsage.value = Math.floor(Math.random() * 30) + 30 // 30-60%
  memoryUsage.value = Math.floor(Math.random() * 40) + 50 // 50-90%
}

// 处理用户命令
const handleUserCommand = async (command) => {
  switch (command) {
    case 'profile':
      ElMessage.info('个人资料功能开发中...')
      break
    case 'settings':
      router.push('/admin/system-settings')
      break
    case 'theme':
      ElMessage.info('主题切换功能开发中...')
      break
    case 'logout':
      try {
        await adminLogout()
        ElMessage.success('退出登录成功')
        router.push('/admin-login')
      } catch (error) {
        console.error('管理员登出失败:', error)
        // 即使登出API失败，也要清除本地状态并跳转
        ElMessage.success('退出登录成功')
        router.push('/admin-login')
      }
      break
  }
}

// 生命周期
onMounted(() => {
  updateNotificationCount()
  // 每5秒更新一次监控数据
  monitorInterval = setInterval(updateMonitorData, 5000)
})

onUnmounted(() => {
  if (monitorInterval) {
    clearInterval(monitorInterval)
  }
})
</script>

<style scoped>
.admin-top-navbar {
  height: 80px;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(15px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 30px;
  position: relative;
  z-index: 10;
}

.navbar-left {
  display: flex;
  align-items: center;
}

.app-title {
  color: #fff;
  font-size: 24px;
  font-weight: 700;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.title-icon {
  color: #00c3ff;
  font-size: 28px;
}

.navbar-center {
  display: flex;
  align-items: center;
}

.system-status {
  display: flex;
  align-items: center;
  gap: 10px;
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.notification-badge {
  margin-right: 10px;
}

.notification-badge :deep(.el-button) {
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  transition: all 0.3s ease;
}

.notification-badge :deep(.el-button:hover) {
  background: rgba(0, 195, 255, 0.2);
  border-color: #00c3ff;
  color: #00c3ff;
}

.user-dropdown {
  cursor: pointer;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 10px;
  color: #fff;
  padding: 8px 15px;
  border-radius: 25px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.user-info:hover {
  background: rgba(0, 195, 255, 0.2);
  border-color: #00c3ff;
  color: #00c3ff;
}

.username {
  font-weight: 500;
  font-size: 14px;
}

.dropdown-icon {
  font-size: 12px;
  transition: transform 0.3s ease;
}

.user-dropdown.is-active .dropdown-icon {
  transform: rotate(180deg);
}

/* 系统监控样式 */
.system-monitor {
  display: flex;
  gap: 15px;
  align-items: center;
}

.monitor-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
}

.monitor-item:hover {
  background: rgba(0, 195, 255, 0.2);
  border-color: #00c3ff;
}

.monitor-icon {
  color: #00c3ff;
  font-size: 16px;
}

.monitor-bar {
  width: 40px;
  height: 6px;
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
  overflow: hidden;
}

.monitor-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.5s ease;
}

.monitor-fill.cpu {
  background: linear-gradient(90deg, #00c3ff, #4A90E2);
}

.monitor-fill.memory {
  background: linear-gradient(90deg, #f39c12, #e74c3c);
}

.monitor-text {
  color: white;
  font-size: 12px;
  font-weight: 500;
  min-width: 30px;
}

/* 通知中心样式 */
.notification-center {
  position: relative;
}

.notification-btn {
  background: rgba(255, 255, 255, 0.1) !important;
  border: 1px solid rgba(255, 255, 255, 0.2) !important;
  color: #fff !important;
  transition: all 0.3s ease;
}

.notification-btn:hover {
  background: rgba(0, 195, 255, 0.2) !important;
  border-color: #00c3ff !important;
  color: #00c3ff !important;
  transform: scale(1.1);
}

.notification-icon {
  transition: all 0.3s ease;
}

.notification-icon.shake {
  animation: shake 0.5s ease-in-out infinite;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
}

/* 通知面板样式 */
.notification-panel {
  max-height: 400px;
  overflow: hidden;
  text-align: left;
  background: rgba(255, 255, 255, 0.98);
  backdrop-filter: blur(10px);
  border-radius: 8px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
}

.notification-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px 0 10px;
  border-bottom: 1px solid #eee;
  font-weight: 600;
  color: #2c3e50;
}

.notification-list {
  max-height: 300px;
  overflow-y: auto;
}

.notification-item {
  display: flex;
  padding: 12px 0;
  border-bottom: 1px solid #f5f5f5;
  cursor: pointer;
  transition: all 0.3s ease;
}

.notification-item:hover {
  background: #f8f9fa;
}

.notification-item.unread {
  background: rgba(0, 195, 255, 0.05);
}

.notification-icon-wrapper {
  margin-right: 12px;
  padding-top: 2px;
}

.notification-success {
  color: #67c23a;
}

.notification-warning {
  color: #e6a23c;
}

.notification-info {
  color: #409eff;
}

.notification-error {
  color: #f56c6c;
}

.notification-content {
  flex: 1;
}

.notification-title {
  font-weight: 500;
  color: #2c3e50;
  margin-bottom: 4px;
}

.notification-desc {
  font-size: 13px;
  color: #7f8c8d;
  margin-bottom: 4px;
}

.notification-time {
  font-size: 12px;
  color: #bdc3c7;
}

/* 用户信息增强样式 */
.user-avatar-wrapper {
  position: relative;
}

.user-avatar {
  border: 2px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}

.user-info:hover .user-avatar {
  border-color: #00c3ff;
}

.online-indicator {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 10px;
  height: 10px;
  background: #67c23a;
  border-radius: 50%;
  border: 2px solid white;
  animation: pulse 2s infinite;
}

.user-details {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.user-role {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1;
  margin-top: 2px;
}

/* 下拉菜单增强样式 */
:deep(.user-dropdown-menu) {
  background: rgba(255, 255, 255, 0.95) !important;
  backdrop-filter: blur(10px);
  border: 1px solid rgba(0, 195, 255, 0.2) !important;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1) !important;
}

:deep(.dropdown-item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  transition: all 0.3s ease;
}

:deep(.dropdown-item:hover) {
  background: rgba(0, 195, 255, 0.1) !important;
  color: #00c3ff !important;
}

:deep(.dropdown-item.logout:hover) {
  background: rgba(245, 108, 108, 0.1) !important;
  color: #f56c6c !important;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .admin-top-navbar {
    padding: 0 15px;
  }
  
  .app-title {
    font-size: 18px;
  }
  
  .navbar-center {
    display: none;
  }
  
  .username {
    display: none;
  }
}

@media (max-width: 480px) {
  .app-title {
    font-size: 16px;
  }
  
  .navbar-right {
    gap: 10px;
  }
}
</style>
